<?php
namespace Home\Model;
use Think\Model;
class GcategoryModel extends Model{
    
    
    function get_list($parent_id = -1, $shown = true,$store_id=0)
    {
        $conditions = "1 = 1";
        $parent_id >= 0 && $conditions .= " AND parent_id = '$parent_id'";
        $shown && $conditions .= " AND if_show = 1";
        $conditions .= " AND store_id = ".$store_id;
		$all_category=$this->where($conditions)->order('sort_order asc')->select();
		$return_val=array();
		if(is_array($all_category)){
			foreach($all_category as $cate){
				$return_val[$cate['cate_id']]=$cate;
			}
		}
        return $return_val;
    }
     /**
     * 取得某分类的所有子孙分类id
     */
    function get_descendant($id,$store_id=0)
    {
        $ids = array($id);
        $this->_get_descendant($ids, $id,$store_id);
        return $ids;
    }
    function _get_descendant(&$ids, $id,$store_id)
    {
        $childs = $this->where("parent_id = ".$id.($store_id?' AND store_id='.$store_id:''))->select();
        foreach ($childs as $child)
        {
            $ids[] = $child['cate_id'];
            $this->_get_descendant($ids, $child['cate_id']);
        }
    } 
}
?>
